//: src/com/silvtar/isync/SSIsync.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 6/5/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * Modify History:
 * Date        Version    Description
 * 6/ 5/2013   0.1        Create The Class
 */

package com.silvtar.isync;

import java.net.Socket;

import com.silvtar.io.StreamToolkit;
import com.silvtar.net.Message;
import com.silvtar.net.Server;
import com.silvtar.net.transporter.FileTransporterServer;
import com.silvtar.sync.model.SSTree;

/**
 * 使用内置传输器的同步类守护
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class SSIsyncDaemon implements Server.Acceptor {

    int isyncCommandPort;
    Server server;
    FileTransporterServer fileTransporterServer;
    
    /**
     * 构造函数，使用本方法构造对象后还需要调用configIsyncDaemon()来配置本类
     */
    public SSIsyncDaemon() {
        
    }
    
    /**
     * 构造函数
     * @param isyncCommandPort isync端口
     * @param commandPort 内置传输器命令端口
     * @param dataPort 内置传输器数据端口
     */
    public SSIsyncDaemon(int isyncCommandPort, int commandPort, int dataPort) {
        configIsyncDaemon(isyncCommandPort, commandPort, dataPort);
    }
    
    /**
     * 配置函数
     * @param isyncCommandPort isync端口
     * @param commandPort 内置传输器命令端口
     * @param dataPort 内置传输器数据端口
     */
    public void configIsyncDaemon(int isyncCommandPort, int commandPort, int dataPort) {
        this.isyncCommandPort = isyncCommandPort;
        server = new Server(this, isyncCommandPort);
        server.listen();
        fileTransporterServer = new FileTransporterServer(commandPort, dataPort);
    }
    
    /**
     * 开启同步守护进程
     */
    public void startIsyncDaemon() {
        fileTransporterServer.startCmdServer();
    }
    
    @Override
    public void accept(int identifier) {
        Socket cmdSocket = server.getSocketList().get(identifier);
        StreamToolkit cmdST = new StreamToolkit(cmdSocket);
        Message message = (Message) cmdST.readObject();
        
        if (message.getType() == Message.SYNC) {
            if (message.getContent().equals("start")) {
                //准备
                String filePath = (String) message.getAccessory();
                SSTree ssTree = new SSTree(filePath);
                Message treeMessage = new Message("filetree", Message.SYNC);
                treeMessage.setAccessory(ssTree);
                cmdST.writeObject(treeMessage);
            }
        }
    }
}
